package com.wskj.workbench.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wskj.common.core.domain.entity.TbUserTiktok;
import com.wskj.common.core.mybatisplus.core.ServicePlusImpl;
import com.wskj.common.core.page.PagePlus;
import com.wskj.common.core.page.TableDataInfo;
import com.wskj.common.utils.common.AssertUtils;
import com.wskj.common.utils.common.PageUtils;
import com.wskj.common.utils.common.SecurityUtils;
import com.wskj.workbench.bo.app.AppTbUserJoinOrderDetailAddBo;
import com.wskj.workbench.bo.app.AppUpdateTiktokBo;
import com.wskj.workbench.bo.web.TbUserTiktokAddBo;
import com.wskj.workbench.bo.web.TbUserTiktokEditBo;
import com.wskj.workbench.bo.web.TbUserTiktokQueryBo;
import com.wskj.workbench.mapper.TbUserTiktokMapper;
import com.wskj.workbench.service.ITbUserTiktokService;
import com.wskj.workbench.vo.web.TbUserTiktokVo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 用户对应的抖音号Service业务层处理
 *
 * @author wskj
 * @date 2024-09-24
 */
@Service
public class TbUserTiktokServiceImpl extends ServicePlusImpl<TbUserTiktokMapper, TbUserTiktok> implements ITbUserTiktokService {

    @Override
    public TbUserTiktokVo queryById(Long id) {
        return getVoById(id, TbUserTiktokVo.class);
    }

    @Override
    public TableDataInfo<TbUserTiktokVo> queryPageList(TbUserTiktokQueryBo bo) {
        PagePlus<TbUserTiktok, TbUserTiktokVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), TbUserTiktokVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<TbUserTiktokVo> queryList(TbUserTiktokQueryBo bo) {
        return listVo(buildQueryWrapper(bo), TbUserTiktokVo.class);
    }

    private LambdaQueryWrapper<TbUserTiktok> buildQueryWrapper(TbUserTiktokQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TbUserTiktok> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, TbUserTiktok::getUserId, bo.getUserId());
        lqw.like(StrUtil.isNotBlank(bo.getTikName()), TbUserTiktok::getTikName, bo.getTikName());
        lqw.eq(StrUtil.isNotBlank(bo.getTikUid()), TbUserTiktok::getTikUid, bo.getTikUid());
        lqw.eq(bo.getTikFans() != null, TbUserTiktok::getTikFans, bo.getTikFans());
        lqw.eq(StrUtil.isNotBlank(bo.getDelFlag()), TbUserTiktok::getDelFlag, bo.getDelFlag());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(TbUserTiktokAddBo bo) {
        TbUserTiktok add = BeanUtil.toBean(bo, TbUserTiktok.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(TbUserTiktokEditBo bo) {
        TbUserTiktok update = BeanUtil.toBean(bo, TbUserTiktok.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(TbUserTiktok entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

    @Override
    public String importTbUserTiktok(List<TbUserTiktokVo> tbUserTiktokList) {
        AssertUtils.isTrue(CollectionUtils.isNotEmpty(tbUserTiktokList), "导入数据不能为空");
        int successNum = 0;
        for (TbUserTiktokVo tbUserTiktok : tbUserTiktokList) {
            TbUserTiktok add = BeanUtil.toBean(tbUserTiktok, TbUserTiktok.class);
            save(add);
            successNum++;
        }
        String msg = String.format("导入完成，共计 %d 条数据，成功 %d 条！", tbUserTiktokList.size(), successNum);
        if (successNum != tbUserTiktokList.size()) {
            msg = msg + String.format("失败 %d 条！", tbUserTiktokList.size() - successNum);
        }
        return msg;
    }

    @Override
    public boolean updateTiktok(AppUpdateTiktokBo appUpdateTiktokBo) {

        AssertUtils.isTrue(ObjectUtil.isNotEmpty(appUpdateTiktokBo.getTiktokList()), "抖音号列表不能为空");

        // 删除原来的抖音信息
        // remove(Wrappers.lambdaQuery(TbUserTiktok.class).eq(TbUserTiktok::getUserId, SecurityUtils.getUserId()));

        List<AppTbUserJoinOrderDetailAddBo> tiktokList = appUpdateTiktokBo.getTiktokList();

        List<TbUserTiktok> list = new ArrayList<>();
        for (AppTbUserJoinOrderDetailAddBo appTbUserJoinOrderDetailAddBo : tiktokList) {
            TbUserTiktok tbUserTiktok = BeanUtil.copyProperties(appTbUserJoinOrderDetailAddBo, TbUserTiktok.class);


            // 查询抖音uid是不是已经绑定到其他用户名下
            long count = count(Wrappers.lambdaQuery(TbUserTiktok.class).eq(TbUserTiktok::getTikUid,
                    tbUserTiktok.getTikUid()).ne(TbUserTiktok::getUserId, SecurityUtils.getUserId()));

            AssertUtils.isTrue(count == 0, "抖音uid【" + tbUserTiktok.getTikUid() + "】,已被其他用户绑定,不能重复绑定");


            tbUserTiktok.setUserId(SecurityUtils.getUserId());
            list.add(tbUserTiktok);
        }
        boolean result = false;
        if (ObjectUtil.isNotEmpty(list)) {
            result = saveAll(list);
        }
        return result;
    }
}